package org.black.phoneUtility.sms.reply.app.fragment;

import java.io.Serializable;

import org.black.phoneUtility.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class SmsReplyAddEditNumberUserInputDialogFragment extends
		DialogFragment {
	public static final String TAG = SmsReplyAddEditNumberUserInputDialogFragment.class
			.getName();
	public static final String ARGUMENTS_SMS_REPLY_ADD_EDIT_NUMBER_PHONE_NUMBER = "addEditPhoneNumber";
	public static final String ARGUMENTS_SMS_REPLY_ADD_EDIT_NUMBER_FROM = "addEditPhoneNumberFrom";

	public static class AddEditPhoneNumberFor implements Serializable {
		private static final long serialVersionUID = 9077543713725028219L;
		public static final AddEditPhoneNumberFor RESPONSE_NUMBER = new AddEditPhoneNumberFor(
				129);
		public static final AddEditPhoneNumberFor REPLY_TO_SPECIFIC = new AddEditPhoneNumberFor(
				130);
		public static final AddEditPhoneNumberFor NONE_REPLY = new AddEditPhoneNumberFor(
				131);

		private int code;

		private AddEditPhoneNumberFor(int code) {
			this.code = code;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + code;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			AddEditPhoneNumberFor other = (AddEditPhoneNumberFor) obj;
			if (code != other.code)
				return false;
			return true;
		}

	}

	public interface SmsReplyAddEditNumberUserInputInterface {
		public void onChooseSmsReplyAddEditNumberUserInputDialogPositiveClick(
				AddEditPhoneNumberFor addEditPhoneNumberFor, String phoneNumber);

		public void onChooseSmsReplyAddEditNumberUserInputDialogNegativeClick();
	}

	private SmsReplyAddEditNumberUserInputInterface smsReplyAddEditNumberUserInputInterface;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			smsReplyAddEditNumberUserInputInterface = (SmsReplyAddEditNumberUserInputInterface) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement SmsReplyAddEditNumberUserInputInterface");
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		super.onCreateDialog(savedInstanceState);
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		final Bundle bundle = getArguments() == null ? savedInstanceState
				: getArguments();
		String phoneNumber = bundle
				.getString(ARGUMENTS_SMS_REPLY_ADD_EDIT_NUMBER_PHONE_NUMBER) == null ? null
				: bundle.getString(ARGUMENTS_SMS_REPLY_ADD_EDIT_NUMBER_PHONE_NUMBER);
		View view = getActivity().getLayoutInflater().inflate(
				R.layout.sms_reply_add_edit_number_user_input_dialog, null);

		final EditText addEditNumberUserInputEditText = (EditText) view
				.findViewById(R.id.smsReplyAddEditNumberUserInputEditText);
		addEditNumberUserInputEditText.setText(phoneNumber);

		builder.setTitle(R.string.sms_add_edit_from_user_input_title)
				.setPositiveButton(android.R.string.ok,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								Object addEditPhoneNumberForObject = bundle
										.getSerializable(ARGUMENTS_SMS_REPLY_ADD_EDIT_NUMBER_FROM);
								AddEditPhoneNumberFor addEditPhoneNumberFor = null;
								if (addEditPhoneNumberForObject != null) {
									addEditPhoneNumberFor = (AddEditPhoneNumberFor) addEditPhoneNumberForObject;
								}
								smsReplyAddEditNumberUserInputInterface
										.onChooseSmsReplyAddEditNumberUserInputDialogPositiveClick(
												addEditPhoneNumberFor,
												addEditNumberUserInputEditText
														.getText().toString());

							}
						})
				.setNegativeButton(android.R.string.cancel,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {

							}
						});

		builder.setView(view);
		return builder.create();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		this.smsReplyAddEditNumberUserInputInterface = null;
	}

}
